﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Kristall.Engine;
using Kristall.MyGame.Behaviors;
using Kristall.Managers;

namespace Kristall.MyGame.Enemies
{
    [Serializable]
    public class TestEnemy : Enemy
    {
        AnimatableSprite idleAnimation;
        AnimatableSprite runAnimation;
        string idle = @"Enemies\Test\Idle.xnb";
        string run = @"Enemies\Test\Run.xnb";

        PatrolBehavior _patrol;

        float gruntTime = 5f;
        const float GruntTime = 5f;
        float gruntTimer = 0;
        Sound2D gruntSound;

        public TestEnemy() : base()
        {
            this.MoveAcceleration = 6000f;
            _patrol = new PatrolBehavior(this);
        }

        public override void LoadContent(Level level)
        {
            idleAnimation = new AnimatableSprite(idle);
            runAnimation = new AnimatableSprite(run);

            Damage = 0.6f;

            Animations.Clear();
            Animations.Add(idleAnimation);
            Animations.Add(runAnimation);
            CurrentAnimationKey = idle;

            base.LoadContent(level);

            CurrentBehavior = _patrol;

            idleAnimation.FrameWidth = idleAnimation.FrameHeight = 64;
            runAnimation.FrameWidth = runAnimation.FrameHeight = 64;

            runAnimation.FrameCount = 10;

            gruntSound = new Sound2D("TrollIdle");
            randomizeGrunt();

            SetSize(32, 54); //idleAnimation.FrameWidth, idleAnimation.FrameHeight);
            idleAnimation.Origin = runAnimation.Origin = new Vector2(16, 10);
        }

        void randomizeGrunt()
        {
            Random r = new Random();
            float random = (float)r.NextDouble() * 2f - 1f;
            gruntTime = 5 + random * 3f;
        }

        void updateGrunt(float dt)
        {
            gruntSound.Update(Level.Player.Center, Center);

            gruntTimer += dt;

            if (gruntTimer >= gruntTime)
            {
                gruntSound.Play();
                gruntTimer = 0;

                randomizeGrunt();
            }
        }

        public override void Update(float dt)
        {
            if (IsAlive && IsOnGround)
            {
                updateGrunt(dt);

                if (Math.Abs(Velocity.X) - 0.02f > 0)
                {
                    CurrentAnimationKey = run;

                    runAnimation.Mirror = false;
                    if (Velocity.X > 0)
                        runAnimation.Mirror = true;
                }
                else
                {
                    CurrentAnimationKey = idle;
                }
            }
            else if (!IsAlive)
            {
                RemoveFromLevel();
                return;
            }

            base.Update(dt);
        }
    }
}
